//
//  ChangeCodeViewController.m
//  XDCommonApp
//
//  Created by wanglong8889@126.com on 14-6-12.
//  Copyright (c) 2014年 XD-XY. All rights reserved.
//

#import "ChangeCodeViewController.h"
#define XZB  20

@interface ChangeCodeViewController ()

@end

@implementation ChangeCodeViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.titleLabel.text = @"修改密码";
    
    [self initChangeCodeViews];
    // Do any additional setup after loading the view.
}


-(void)initChangeCodeViews{
    
    mScrollView =[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, UI_SCREEN_HEIGHT -UI_STATUS_BAR_HEIGHT -UI_STATUS_BAR_HEIGHT)];
    mScrollView.contentSize=CGSizeMake(UI_SCREEN_WIDTH, UI_SCREEN_HEIGHT -UI_STATUS_BAR_HEIGHT );
    mScrollView.scrollEnabled =YES;
    mScrollView.delegate = self;
    mScrollView.showsVerticalScrollIndicator = NO;
    [self.contentView addSubview:mScrollView];
    
    UIImageView *oldView = [[UIImageView alloc] initWithFrame:CGRectMake(XZB, 20, 281, 41)];
    [oldView setImage:[UIImage imageNamed:@"input_kuag"]];
    oldView.userInteractionEnabled = YES;
    
    [self.contentView addSubview:oldView];
    
    oldTf = [[UITextField alloc] initWithFrame:CGRectMake(5, 5,271, 31)];
    oldTf.borderStyle = UITextBorderStyleNone;
    oldTf.backgroundColor =  [UIColor clearColor];
    oldTf.placeholder = @"请输入现在的密码";
    oldTf.returnKeyType = UIReturnKeyDone;
    oldTf.textColor = [UIColor grayColor];
    
    
    oldTf.delegate = self;
    oldTf.font = [UIFont systemFontOfSize:15.0];
    oldTf.clearButtonMode = UITextFieldViewModeWhileEditing;
    [oldView addSubview:oldTf];
    
    
    
    //新密码
    UIImageView *newView = [[UIImageView alloc] initWithFrame:CGRectMake(XZB, 20+oldView.frame.origin.y+oldView.frame.size.height, 281, 41)];
    [newView setImage:[UIImage imageNamed:@"input_kuag"]];
    newView.userInteractionEnabled = YES;
    
    [self.contentView addSubview:newView];
    
    newTf = [[UITextField alloc] initWithFrame:CGRectMake(5, 5,271, 31)];
    newTf.borderStyle = UITextBorderStyleNone;
    newTf.backgroundColor =  [UIColor clearColor];
    newTf.placeholder = @"请输入新密码";
    newTf.returnKeyType = UIReturnKeyDone;
    newTf.textColor = [UIColor grayColor];
    
    
    newTf.delegate = self;
    newTf.font = [UIFont systemFontOfSize:15.0];
    newTf.clearButtonMode = UITextFieldViewModeWhileEditing;
    [newView addSubview:newTf];
    
    
    
    //确认密码
    
    UIImageView *confirmView = [[UIImageView alloc] initWithFrame:CGRectMake(XZB, 20+newView.frame.origin.y+newView.frame.size.height, 281, 41)];
    [confirmView setImage:[UIImage imageNamed:@"input_kuag"]];
    confirmView.userInteractionEnabled = YES;
    
    [self.contentView addSubview:confirmView];
    
    confirmTf = [[UITextField alloc] initWithFrame:CGRectMake(5, 5,271, 31)];
    confirmTf.borderStyle = UITextBorderStyleNone;
    confirmTf.backgroundColor =  [UIColor clearColor];
    confirmTf.placeholder = @"请输入确认密码";
    confirmTf.returnKeyType = UIReturnKeyDone;
    confirmTf.textColor = [UIColor grayColor];
    
    
    confirmTf.delegate = self;
    confirmTf.font = [UIFont systemFontOfSize:15.0];
    confirmTf.clearButtonMode = UITextFieldViewModeWhileEditing;
    [confirmView addSubview:confirmTf];
    
    [mScrollView addSubview:oldView];
    
    [mScrollView addSubview:newView];
    
    [mScrollView addSubview:confirmView];
    
    
    //修改按钮
    UIButton *getVerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    getVerBtn.frame = CGRectMake(XZB, confirmView.frame.origin.y+confirmView.frame.size.height+50, 281, 40);
    [getVerBtn setBackgroundImage:[UIImage imageNamed:@"launch_btn"] forState:UIControlStateNormal];
    [getVerBtn setBackgroundImage:[UIImage imageNamed:@"launch_btn"] forState:UIControlStateHighlighted];
    [getVerBtn addTarget:self action:@selector(changCodeAction:) forControlEvents:UIControlEventTouchUpInside];
    [getVerBtn setTitle:@"修改密码" forState:UIControlStateNormal];
    [getVerBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    getVerBtn.titleLabel.font = [UIFont systemFontOfSize:15.0];
    
    [mScrollView addSubview:getVerBtn];
    
}



//修改密码
-(void)changCodeAction:(UIButton *)btn{
    if (!IS_NOT_EMPTY(oldTf.text)||!IS_NOT_EMPTY(newTf.text)||!IS_NOT_EMPTY(confirmTf.text)) {
        [XDTools showTips:@"信息不能为空" toView:self.contentView];
    }else{
        NSString *oldPass = [oldTf.text MD5Hash];
        NSString *newPass = [newTf.text MD5Hash];
        if ([oldPass isEqualToString:newPass]) {
            [XDTools showTips:@"新密码和旧密码不能相同" toView:self.contentView];
        }else{
            if([newTf.text isEqualToString:confirmTf.text]){
                NSDictionary *dic = @{@"userName":@"13767866",
                                      @"password":oldPass,
                                      @"newPassword":newPass,
                                      @"securitySignal":@"3765673"};
                
                [self changeCodeParms:dic];
            }else{
                [XDTools showTips:@"两次密码不一致" toView:self.contentView];
            }
        }
        
        
    }
}


//修改密码
- (void)changeCodeParms:(NSDictionary *)parms{
    
    __weak  ASIFormDataRequest *request = [XDTools postRequestWithDict:parms API:API_CHANGECODE];
    
    [request setCompletionBlock:^{
        [XDTools hideProgress:self.view];
        
        NSDictionary *tempDic = [XDTools  JSonFromString:[request responseString]];
        if([[tempDic objectForKey:@"result"]intValue] == 1){
            
            [XDTools showTips:@"修改密码成功" toView:self.view];
            
        }else{
            [XDTools showTips:tempDic[@"msg"] toView:self.view];
            
        }
    }];
    
    [request setFailedBlock:^{
        
        [XDTools hideProgress:self.view];
        NSError *error = [request error];
        DDLOG(@"error=%@",error);
    }];
    
    [XDTools showProgress:self.view];
    [request startAsynchronous];
    
}



- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [oldTf resignFirstResponder];
    [newTf resignFirstResponder];
    [confirmTf resignFirstResponder];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
